<?php

// +----------------------------------------------------------------------
// | 权限组控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\pay\admin;

use app\model\pos\Pos as PosModel;
use app\model\ucenter\User as UserModel;
use yicmf\builder\Edit;
use yicmf\builder\Table;
use app\controller\admin\Common;
use app\event\file\Attachment as AttachmentEvent;
use app\model\ucenter\UserMoneyLog as UserMoneyLogModel;
use app\model\ucenter\UserCash as UserCashModel;
use app\event\pay\UserCash as UserCashEvent;

class UserCash extends Common
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function _filter()
    {
        $where[] = ['status', '>', -2];
        return $where;
    }

    public function index()
    {

        $statusOptions = [
            [
                'id' => 1,
                'value' => '未划拨'
            ],
            [
                'id' => 2,
                'value' => '已划拨'
            ]
        ];
        $where = $this->_filter();
        $field = $this->request->param('field/a');
        if (!empty($field['is_bind'])) {
            if ($field['is_bind'] == 2) {
                $where[] = ['user_id', '!=', ''];
            } else {
                $where[] = ['user_id', '=', null];
            }
        }
        if (!empty($field['sn_start'])) {
            $where[] = ['sn', '>=', $field['sn_start']];
        }
        if (!empty($field['sn_end'])) {
            $where[] = ['sn', '<=', $field['sn_end']];
        }

        //ID 姓名 手机号 等级  提现金额 手续费 提现费 实际入账  审核状态 驳回原因 申请时间  审核时间 事项 时间 资金类型
        // 显示页面
        $builder = new Table();
        $builder->model(UserCashModel::class)
            ->where($where)
            ->searchUser('用户')
//                ->suggest('高等级可以对低等级用户进行操作或者授权')
            ->searchTextLike('memo', '备注')
            ->searchDateTimeRange('create_time','变更时间')
            ->excel()
//                ->suggest('高等级可以对低等级用户进行操作或者授权')
//            ->searchSelect('is_bind', '划拨状态', $statusOptions)
//            ->searchText('sn_start', '机身开始号')
//            ->searchText('sn_end', '机身结束号')
//            ->searchText('result_count', '显示总数')
//            ->keyLeftLeader('checkbox')
            ->keyId()
            ->keyText('user.nickname', '用户名')
            ->keyText('user.mobile', '手机号')
            ->keyText('user.level', '等级')
            ->keyText('cash_money', '提现金额')
            ->keyText('cash_charge', '手续费')
//            ->keyText('after', '提现费')
            ->keyText('real_cash', '实际入账')
            ->keyMap('status', '审核状态',['未审核','已审核','已驳回'])
            ->keyText('reject', '驳回原因')
//            ->keyMap('type', '资金类型',UserMoneyLogModel::$money_type)
//            ->keyText('order_id', '关联订单')
				->keyCreateTime('提交时间')
                ->keyTime('audit_time','审核时间')
            ->actionUpdate('update?id={$id}', '审核', [0]);
        return $builder->fetch();
    }


    public function update()
    {
        if ($this->request->isPost()) {
            $event = new UserCashEvent();
            $data= $event->check($this->request->param(),$this->user);
            if (0 === $data['code']) {
                $this->_returnSuccess($data['message']);
            } else {
                $this->_returnError($data['message'], false);
            }
        } else {
            // 同步节点与菜单
            if ($this->request->param('id')) {
                $group_id = $this->request->param('id');
                $data = UserCashModel::where('id', $group_id)->where('status',0)->find();
                if (!$data) {
                    $this->_returnError('审核记录不存在');
                }
            } else {
                $this->_returnError('审核记录不存在');
            }
            $data['status'] = 1;
            $builder = new Edit();
            $builder->title('提现审核')
                ->keyLabel('user.nickname', '用户名')
                ->keyLabel('user.username', '手机号')
                ->keyLabel('user.level', '等级')
                ->keyLabel('real_name', '提现姓名')
                ->keyLabel('bank_no', '提现账号')
                ->keyLabel('cash_money', '提现金额')
                ->keyTextInline('cash_charge', '手续费','可修改')
                ->keyRadio('status','审核结果',[1=>'通过',2=>'驳回'])
                ->keyText('reject', '驳回原因')
                ->data($data)
                ->setTrigger('status',1,'')
                ->setTrigger('status',2,'reject')
                ->buttonSubmit();
            return $builder->fetch();
        }
    }



}
